package com.xianyun.site.iblog.gatewayimpl;

import com.xianyun.common.core.utils.DateToolUtils;
import com.xianyun.common.core.utils.bean.BeanUtils;
import com.xianyun.site.iblog.convertor.ArticleConvertor;
import com.xianyun.site.iblog.domain.blog.gateway.ArticleGateWay;
import com.xianyun.site.iblog.domain.blog.model.Article;
import com.xianyun.site.iblog.dto.blog.data.ColumnDTO;
import com.xianyun.site.iblog.dto.blog.data.TagsDTO;
import com.xianyun.site.iblog.gatewayimpl.database.ArticleMapper;
import com.xianyun.site.iblog.gatewayimpl.database.ColumnMapper;
import com.xianyun.site.iblog.gatewayimpl.database.TagsMapper;
import com.xianyun.site.iblog.gatewayimpl.database.domainobject.ArticleDO;
import com.xianyun.site.iblog.gatewayimpl.database.domainobject.ColumnDO;
import com.xianyun.site.iblog.gatewayimpl.database.domainobject.TagsDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.List;

/**
 * 文章领域网关实现
 * @author mac_zyj
 */
@Component
public class ArticleGatewayImpl implements ArticleGateWay {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TagsMapper tagsMapper;
    @Autowired
    private ColumnMapper columnMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addArticle(Article article) {
        ArticleDO saveDate = ArticleConvertor.toDataObject(article);
        articleMapper.saveArticle(saveDate);
        listAndSetTagsAndColumn(article,saveDate.getId(),Integer.parseInt(saveDate.getUid()));
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateArticle(Article article) {
        ArticleDO updateDate = ArticleConvertor.toDataObject(article);
        articleMapper.updateArticle(updateDate);
        columnMapper.deleteColumnsInBlog(updateDate.getId());
        tagsMapper.deleteTagsInBlog(updateDate.getId());
        listAndSetTagsAndColumn(article,article.getId(),Integer.parseInt(article.getUid()));
    }
    /***
     * 保存标签和专栏
     * @param article 数据
     * @param blogId 博客ID
     * @param uid 用户ID
     */
    private void listAndSetTagsAndColumn(Article article, Integer blogId, Integer uid){
        if (null != article.getTags()){
            for (TagsDTO blogTags:article.getTags()){
                checkAndSetTags(blogTags,blogId,uid);
            }
        }
        if (null != article.getColumns()){
            Assert.isTrue(article.getColumns().size()<=5,"最多只能绑定5个专栏");
            for (ColumnDTO column:article.getColumns()){
                checkAndSetClassifiedColumn(column,blogId,uid);
            }
        }
    }
    private void checkAndSetTags(TagsDTO tagDTO, Integer blogId, Integer uid){
        TagsDO searchData=TagsDO.builder().tagName(tagDTO.getTagName()).uid(uid).build();
        List<TagsDO> tagsList = tagsMapper.selectTagsList(searchData);
        if (tagsList.size()==0){
            tagDTO.setUid(uid);
            TagsDO saveData =new TagsDO();
            BeanUtils.copyProperties(tagDTO,saveData);
            saveData.setCreateTime(DateToolUtils.getNowDate());
            tagsMapper.saveTags(saveData);
        }else {
            tagDTO.setId(tagsList.get(0).getId());
        }
        tagsMapper.saveTageInArticle(uid,tagDTO.getId(),blogId);
    }
    private void checkAndSetClassifiedColumn(ColumnDTO column, Integer blogId, Integer uid){
        ColumnDO searchData=ColumnDO.builder().className(column.getClassName()).uid(uid).build();
        List<ColumnDO> columnList = columnMapper.selectColumnList(searchData);
        if (columnList.size()==0){
            column.setUid(uid);
            ColumnDO saveDate = new ColumnDO();
            BeanUtils.copyProperties(column,saveDate);
            saveDate.setCreateTime(DateToolUtils.getNowDate());
            columnMapper.saveColumn(saveDate);
        }else {
            column.setId(columnList.get(0).getId());
        }
        columnMapper.saveArticleColumn(uid,column.getId(),blogId);
    }
}
